/*******************************************************************************
 * 
 * @Author: 	Seppe Staes (06/2013) 
 * @Class: 		main.js
 * 
 * @Purpose: 	Main entry point of the application Responsibilities: Routing (cf
 * 				Controller 'dispatcher') Initial View instantiation Load templates
 * 
 * TODO: 		http://tinnedfruit.com/2011/03/03/testing-backbone-apps-with-jasmine-sinon.html
 * 
 ******************************************************************************/

window.Router = Backbone.Router.extend({

	/**
	 * URL Routing, acts as controller in BB context
	 * cf. ServletDispatcher > Controller in restful context
	 * 
	 * TODO: 
	 * 
	 * 		"wines" 								: View all wines in the catalogue
	 * 		"wines/:id"								: View details of a specific wine
	 * 		"start"									: Landing page of the application
	 * 		"news"									: View all news articles
	 * TODO: 
	 * 
	 * NOTE! If opened locally, use Firefox 
	 */
	routes : {
		"" : "start",
		"wines/:id"         : "wineDetail",
	},

	/**
	 * Initialize
	 * 		View Components
	 * 		Load Data (Collection.fetch)
	 * 
	 * TODO: Define things to preload
	 * 			> Bootstrapped i18n files
	 * 			> HTML templates
	 * 			> Dashboard models
	 * 
	 * Note: 	The DashboardView is a placeholder view
	 * 			Overall layout that contains widgets
	 * 
	 * TODO:	28/06 	
	 * 
	 * 			Landing page view
	 * 			Wine model
	 * 			Newsarticle model
	 * 			Wine view
	 * 			Newsarticle view
	 * 			Application HTML template
	 * 			WinesList HTML template
	 * 			NewsList HTML template
	 * 
	 * 			Dummy wines from json
	 * 			Dummy news articles from json
	 * 			Replace the circular preloader with image and logo (cf. linkedin)
	 * 
	 * TODO
	 * 
	 */
	initialize : function() {
		
		// Mock application preloader with timer
		setTimeout(function() {
			
			// initially hide body (for opacity tween later on)
			$('body').css({'opacity':'0'});
			
			// load the landing page of the application
			
			
			// load the application layout (container placeholders)
			// this.containerView = new AppContainerLayoutView();
			// $('body').html(this.containerView.render().el);
			
			// fade-in body
			$('body').animate({opacity: 1}, 500);
						
		}, 1200)

		
		
	},

	/* HANDLE ROUTES ******************************************/
	
	/**
	 * Dashboard
	 * 
	 * TODO: The dashboard view should be reponsible for building itself from scratch
	 * instead of instantiating the widgetList here manually
	 * 
	 * TODO: Check loaded state! if app has been loaded, then load dashboard without bootstrap
	 */
	start : function() {
		window.store.populate();
		
		/*
		var dashboardView = new DashboardView();
		// Replace the dashboardView with edossierContextView
    	$('#content').empty();
    	// Load the context page
		$('#content').html(dashboardView.render().el);
		
		// TODO
		this.widgetListView = new WidgetListView({model: window.edossiers});
		$('#edossier-list').html(widgetListView.render().el);
		*/
	},
	
	/**
	 * Create eDossier Context page and populate data
	 * 		
	 * TODO: in Context page, count functions to be displayed in tabs
	 * 	Transition
	 * 		Loader + loading text in header
	 * 		Disable content pane
	 * 		Load eDossier context page
	 * 
	 * TODO: Abstraction of Context page
	 * 		Container for tab menu on the left
	 * 		Container for button bar
	 * 		Container for form/table/etc.
	 * 
	 * TODO: Update the breadcrumb
	 * 
	 * TODO: Mock the template loading
	 * 		Add preloader in header with message
	 * 		Then show the context page with tween fade-in opacity
	 */
	wineDetail : function(id){
		
//		var dossier = window.edossiers.get(id);
//		var dossierContextView = new DossierContextView();
//		
//		// replace the dashboardView with edossierContextView
//    	$('#content').empty();
//
//    	// load the context page
//		$('#content').html(dossierContextView.render().el);
		
	},
	
});

/**
 * Load HTML Templates
 */
templateLoader.load([ 	"TabletView"
                      	],
		function() {
			app = new Router();
			Backbone.history.start();
		});